package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.exception.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetMealMapper;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {
@Autowired
  private SetMealDishService setMealDishService;
    @Autowired

    private SetMealMapper setMealMapper;


    /**
     * 添加套餐信息及其对应的菜品信息
     * @param setmealDto
     */
   @Override
   @Transactional
    public void saveSetMealAndDish(SetmealDto setmealDto) {
        //1.添加套餐基本信息
        save(setmealDto);
        //2.添加套餐对应的菜品信息
       List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
       for (SetmealDish setmealDish : setmealDishes) {
           //将菜品的id与套餐的id设置在一起
           setmealDish.setSetmealId(setmealDto.getId());
       }
       setMealDishService.saveBatch(setmealDishes);
   }

    /**
     *套餐管理分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public IPage<SetmealDto> getPage(Integer page, Integer pageSize, String name) {
        // 构造一个分页对象,指定分页参数
        IPage<SetmealDto> p = new Page<>(page, pageSize);
        // 构造查询条件
        QueryWrapper<Setmeal> qw = new QueryWrapper<>();
        // 名称的模糊查询
        qw.like(StringUtils.isNotBlank(name), "setmeal.name", name);
        // 按照创建时间降序排列
        qw.orderByDesc("create_time");
        //page(p, qw);
        // 调用了自定义的分页条件查询方法
        setMealMapper.getPageAndCategoryName(p, qw);
        return p;
    }

    /**
     * 批量删除或者删除
     * @param ids
     */
    @Override
    public void deleteSetMealAndDish(List<Long> ids) {
        //1.遍历获取每一个套餐ID
        for (Long SetMealId : ids) {
            //2.判断是否存在启售的套餐,如果又抛异常
            //select*form setMeal where id=xxx and status=1
            LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
            qw.eq(Setmeal::getId,SetMealId);
            qw.eq(Setmeal::getStatus,1);
            Setmeal setmeal = getOne(qw);

            if (setmeal!=null){
              //该套餐是启售状态,抛异常
              throw new CustomException("该套餐还在启售状态,请停售后再删除");
            }
        }

        //3.如果都是停售状态则可以执行套餐的删除
       removeByIds(ids);

        //4.继续删除对应的菜品信息
        for (Long SetMealId : ids) {
            //select *from setMeal_dish where setMeal_id=xxx;
            LambdaQueryWrapper<SetmealDish> qw=new LambdaQueryWrapper<>();
            qw.eq(SetmealDish::getDishId,SetMealId);
           setMealDishService.remove(qw);
        }
    }

    @Override
    public List<Setmeal> getSetMealListByCid(Setmeal setmeal) {
        //构造条件查询对象
        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper();
        qw.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        qw.eq(Setmeal::getStatus,setmeal.getStatus());
        return  list(qw);
    }


}
